﻿using System;
using System.Collections.Generic;
using com.huigo.B2B.Web.alipay;
using System.Collections.Specialized;
using com.huigo.Framework.ServiceUtils;

namespace com.huigo.B2B.Web.pay.alipay
{
    public partial class Return : System.Web.UI.Page
    {
        private string orderNo = string.Empty;
        private string payId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            SortedDictionary<string, string> sPara = GetRequestGet();

            if (sPara.Count > 0)//判断是否有带返回参数
            {
                AlipayNotify aliNotify = new AlipayNotify();
                bool verifyResult = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"]);

                if (verifyResult)//验证成功
                //if (true)
                {
                    //商户订单号
                    //string out_trade_no = "2013523125827552-46910010";
                    string out_trade_no = Request.QueryString["out_trade_no"];

                    //支付宝交易号
                    //string trade_no = "2013052366864444";
                    string trade_no = Request.QueryString["trade_no"];

                    //交易状态
                    //string trade_status = "TRADE_FINISHED";
                    string trade_status = Request.QueryString["trade_status"];

                    if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS")
                    {
                        PayInfo payInfo = PayService.InsertAlipay(out_trade_no, trade_no);
                        ShowMsg(PayService.ReturnSupplement(payInfo));
                    }
                    else
                    {
                        ShowMsg(false);
                    }

                }
                else//验证失败
                {
                    ShowMsg(false);
                }
            }
            else
            {
                ShowMsg(false);
            }
        }

        /// <summary>
        /// 获取支付宝GET过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public SortedDictionary<string, string> GetRequestGet()
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
            }

            return sArray;
        }

        private void ShowMsg(bool isSuc)
        {
            string strMsg = "<h1><span style=\"color: Red;\">" + orderNo + "</span>支付成功!</h1>";
            if (!isSuc)
                strMsg = "<h1>支付失败!</h1>";
            msg.Text = strMsg;
        }
    }
}
